package com.ophiux.cs.infrastructure.repositories.chat;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.ophiux.cs.domain.chat.model.entity.CSChatMessageSend;
import com.ophiux.cs.domain.chat.model.entity.CSChatSession;
import com.ophiux.cs.domain.chat.model.valueobj.CSChatMessageQuery;
import com.ophiux.cs.domain.chat.repositories.ICSChatMessageSendRepository;
import com.ophiux.cs.infrastructure.mapper.chat.CSChatMessageSendMapper;
import com.ophiux.cs.infrastructure.mapper.chat.CSMessageInfoMapper;
import com.ophiux.utils.exception.OphiuxDaoException;
import com.ophiux.utils.exception.OphiuxRepositoryException;

@Repository("cSChatMessageSendRepository")
public class CSChatMessageSendRepository implements ICSChatMessageSendRepository{

	@Autowired
	private CSChatMessageSendMapper cSChatMessageSendMapper;	//消息发送记录
	
	@Autowired
	private CSMessageInfoMapper cSMessageInfoMapper;	//聊天内容
	
	@Override
	public void save(CSChatMessageSend model) throws OphiuxRepositoryException {
		try {
			//判断用户是否存在，存在，更新；不存在，新增
			CSChatMessageSend _model = 
					cSChatMessageSendMapper.getCSChatMessageSendBySendID(model.getSEND_ID());
			
			if( _model == null ) {
				//不存在，新增
				cSChatMessageSendMapper.insert(model);
			}else {
				//存在，更新用户
				cSChatMessageSendMapper.update(model);
			}
			
		} catch (OphiuxDaoException e) {
			e.printStackTrace();
			throw new OphiuxRepositoryException("聊天记录存储异常", e);
		}
	}

	@Override
	public CSChatMessageSend getCSChatMessageSendBySendID(String sendID) throws OphiuxRepositoryException {
		try {
			return cSChatMessageSendMapper.getCSChatMessageSendBySendID(sendID);
		} catch (OphiuxDaoException e) {
			e.printStackTrace();
			throw new OphiuxRepositoryException("聊天记录获取异常", e);
		}
	}

	@Override
	public CSChatMessageSend getCSChatMessageSendByMsgID(String msgID) throws OphiuxRepositoryException {
		try {
			return cSChatMessageSendMapper.getCSChatMessageSendBySendID(msgID);
		} catch (OphiuxDaoException e) {
			e.printStackTrace();
			throw new OphiuxRepositoryException("聊天记录获取异常", e);
		}
	}

	@Override
	public List<CSChatMessageSend> getCSChatMessageSends(CSChatMessageQuery query) throws OphiuxRepositoryException {
		try {
			return cSChatMessageSendMapper.getCSChatMessageSends(query);
		} catch (OphiuxDaoException e) {
			e.printStackTrace();
			throw new OphiuxRepositoryException("聊天记录获取异常", e);
		}
	}

}
